﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Xml.Linq;

namespace Noris.Tools.SystemResourceMonitor
{
    /// <summary>
    /// Třída, která obsahuje extenze .NET tříd = rozšíření základní funkcionality
    /// </summary>
    public static class NetExtensions
    {
        #region XElement
        public static void AddAttribute(this XElement element, string attributeName, string value)
        {
            element.Add(new XAttribute(attributeName, (value == null ? "" : value)));
        }
        public static void AddAttribute(this XElement element, string attributeName, int value)
        {
            element.Add(new XAttribute(attributeName, Convertor.Int32ToString(value)));
        }
        public static void AddAttribute(this XElement element, string attributeName, decimal value)
        {
            element.Add(new XAttribute(attributeName, Convertor.DecimalToString(value)));
        }
        public static void AddAttribute(this XElement element, string attributeName, bool value)
        {
            element.Add(new XAttribute(attributeName, (value ? "1" : "0")));
        }
        public static void AddAttribute(this XElement element, string attributeName, DateTime value)
        {
            element.Add(new XAttribute(attributeName, Convertor.DateTimeToString(value)));
        }
        public static void AddAttribute(this XElement element, string attributeName, Rectangle value)
        {
            element.Add(new XAttribute(attributeName, Convertor.RectangleToString(value)));
        }
        public static string GetAttributeString(this XElement element, string attributeName)
        {
            XAttribute attr = GetXAttribute(element, attributeName);
            if (attr == null) return null;
            return attr.Value;
        }
        public static Int32 GetAttributeInt32(this XElement element, string attributeName)
        {
            XAttribute attr = GetXAttribute(element, attributeName);
            if (attr == null) return 0;
            return (int)Convertor.StringToInt32(attr.Value);
        }
        public static decimal GetAttributeDecimal(this XElement element, string attributeName)
        {
            XAttribute attr = GetXAttribute(element, attributeName);
            if (attr == null) return 0m;
            return (decimal)Convertor.StringToDecimal(attr.Value);
        }
        public static bool GetAttributeBoolean(this XElement element, string attributeName)
        {
            XAttribute attr = GetXAttribute(element, attributeName);
            if (attr == null) return false;
            return (attr.Value == "1");
        }
        public static DateTime GetAttributeDateTime(this XElement element, string attributeName)
        {
            XAttribute attr = GetXAttribute(element, attributeName);
            if (attr == null) return DateTime.MinValue;
            return (DateTime)Convertor.StringToDateTime(attr.Value);
        }
        public static Rectangle GetAttributeRectangle(this XElement element, string attributeName)
        {
            XAttribute attr = GetXAttribute(element, attributeName);
            if (attr == null) return Rectangle.Empty;
            return (Rectangle)Convertor.StringToRectangle(attr.Value);
        }

        private static XAttribute GetXAttribute(XElement element, string attributeName)
        {
            if (element == null) return null;
            XAttribute attr = element.Attribute(attributeName);
            return attr;
        }
        public static void Save(this XDocument xDoc, string file, bool textFormat)
        {
            System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
            settings.ConformanceLevel = System.Xml.ConformanceLevel.Document;
            settings.Encoding = Encoding.UTF8;
            settings.Indent = textFormat;
            settings.IndentChars = "  ";
            settings.NewLineHandling = System.Xml.NewLineHandling.Entitize;
            settings.NewLineChars = Environment.NewLine;
            settings.NewLineOnAttributes = false;
            settings.OmitXmlDeclaration = false;
            using (System.Xml.XmlWriter writter = System.Xml.XmlWriter.Create(file, settings))
            {
                xDoc.Save(writter);
            }
        }
        #endregion
    }
}
